package com.wanjia.estate.service.bdservice.zlfjyspireservice;

import com.wanjia.estate.pojo.entity.ZlfInform;
import com.wanjia.estate.pojo.entity.skxx;
import com.wanjia.estate.properties.FileTemplateProperties;
import com.wanjia.estate.utils.MD5Util;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Service
public class ExcelReaderWriterZlfService {

    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    public String ExcelReaderWriter(ZlfInform zlfInform,String folder) {
        String md5FileName = MD5Util.getMD5(zlfInform.getSfzjhm());
        try {
            FileInputStream inputStream = new FileInputStream(fileTemplateProperties.getZlfjysbdpath());
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            //承受方信息
            Row row7 = sheet.getRow(6);
            row7.getCell(0).setCellValue(zlfInform.getNsrsbh());
            row7.getCell(2).setCellValue(zlfInform.getNsrmc());
            row7.getCell(3).setCellValue(zlfInform.getSfzjzl());
            row7.getCell(4).setCellValue(zlfInform.getSfzjhm());
            row7.getCell(6).setCellValue(zlfInform.getGyfs());
            row7.getCell(7).setCellValue(zlfInform.getSzfe());
            row7.getCell(8).setCellValue(zlfInform.getFwtc());
            row7.getCell(9).setCellValue(zlfInform.getSfsyxwqyzc());
            row7.getCell(10).setCellValue(zlfInform.getJszcsyzt());
            row7.getCell(11).setCellValue(zlfInform.getLxdh());

            //申报信息
            Row row11 = sheet.getRow(10);
            row11.getCell(1).setCellValue(zlfInform.getFybh());
            row11.getCell(4).setCellValue(zlfInform.getBdcdyhm());
            row11.getCell(8).setCellValue(zlfInform.getJzmj());
            row11.getCell(10).setCellValue(zlfInform.getFwdz());

            Row row13 = sheet.getRow(12);
            row13.getCell(1).setCellValue(zlfInform.getHtje());
            row13.getCell(4).setCellValue(zlfInform.getDj());
            row13.getCell(8).setCellValue(zlfInform.getHtjesfhs());
            row13.getCell(10).setCellValue(zlfInform.getPgjg());

            Row row15 = sheet.getRow(14);
            row15.getCell(1).setCellValue(zlfInform.getHtqdrq());
            row15.getCell(4).setCellValue(zlfInform.getQsdjrq());
            row15.getCell(8).setCellValue(zlfInform.getQszydx());
            row15.getCell(10).setCellValue(zlfInform.getQszyfs());

            //税款信息
            List<skxx> skxxList = zlfInform.getSkxxList();
            Row row22 = sheet.getRow(21);
            skxx sk = skxxList.get(0);
            row22.getCell(0).setCellValue(sk.getNsrmc());
            row22.getCell(1).setCellValue(sk.getZsxm());
            row22.getCell(2).setCellValue(sk.getJsyj());
            row22.getCell(3).setCellValue(sk.getSl());
            row22.getCell(4).setCellValue(sk.getYnsr());
            row22.getCell(5).setCellValue(sk.getJmgfjmse());
            row22.getCell(6).setCellValue(sk.getQtjmse());
            row22.getCell(8).setCellValue(sk.getXwqyjze());
            row22.getCell(10).setCellValue(sk.getBqybtse());


            Row row24 = sheet.getRow(23);
            if (skxxList.size()>1){
                skxx sk1 = skxxList.get(1);
                row24.getCell(0).setCellValue(sk1.getNsrmc());
                row24.getCell(1).setCellValue(sk1.getZsxm());
                row24.getCell(2).setCellValue(sk1.getJsyj());
                row24.getCell(3).setCellValue(sk1.getSl());
                row24.getCell(4).setCellValue(sk1.getYnsr());
                row24.getCell(5).setCellValue(sk1.getJmgfjmse());
                row24.getCell(6).setCellValue(sk1.getQtjmse());
                row24.getCell(8).setCellValue(sk1.getXwqyjze());
                row24.getCell(10).setCellValue(sk1.getBqybtse());

                Row row26 = sheet.getRow(25);
                row26.getCell(2).setCellValue(Double.parseDouble(sk.getJsyj())+Double.parseDouble(sk1.getJsyj()));
                row26.getCell(4).setCellValue(Double.parseDouble(sk.getYnsr())+Double.parseDouble(sk1.getYnsr()));
                row26.getCell(5).setCellValue(Double.parseDouble(sk.getJmgfjmse())+Double.parseDouble(sk1.getJmgfjmse()));
                row26.getCell(6).setCellValue(Double.parseDouble(sk.getQtjmse())+Double.parseDouble(sk1.getQtjmse()));
                row26.getCell(8).setCellValue(Double.parseDouble(sk.getXwqyjze())+Double.parseDouble(sk1.getXwqyjze()));
                row26.getCell(10).setCellValue(Double.parseDouble(sk.getBqybtse())+Double.parseDouble(sk1.getBqybtse()));
            }else{
                Row row26 = sheet.getRow(25);
                row26.getCell(2).setCellValue(Double.parseDouble(sk.getJsyj()));
                row26.getCell(4).setCellValue(Double.parseDouble(sk.getYnsr()));
                row26.getCell(5).setCellValue(Double.parseDouble(sk.getJmgfjmse()));
                row26.getCell(6).setCellValue(Double.parseDouble(sk.getQtjmse()));
                row26.getCell(8).setCellValue(Double.parseDouble(sk.getXwqyjze()));
                row26.getCell(10).setCellValue(Double.parseDouble(sk.getBqybtse()));
            }


            FileOutputStream outputStream = new FileOutputStream(folder+"/"+md5FileName + "zlf.xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        File file = new File(folder+"/"+md5FileName + "zlf.xlsx");
        return file.getPath();
    }
}
